package com.pan.controller;


import com.pan.dao.coupon.CouponDao;
import com.pan.dao.coupon.CouponSelectDao;
import com.pan.entity.Coupon;
import com.pan.entity.Result;
import com.pan.service.CouponService;
import com.pan.vo.CouponCreateVo;
import com.pan.vo.CouponShowVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Controller
public class CouponController {
    @Resource
    private CouponService couponService;


    @ResponseBody
    @GetMapping("/coupon/select")
    public Result<List<Coupon>> select(){
        Result<List<Coupon>> result = couponService.select();
        return result;
    }

    @ResponseBody
    @GetMapping("/coupon/select/id/{id}")
    public Result<CouponShowVo> selectById(@PathVariable("id") Long id){
        Result<CouponShowVo> result = couponService.selectShowVoById(id);
        return result;
    }


    @ResponseBody
    @GetMapping("/coupon/select/category/{categoryId}")
    public Result<List<Coupon>> selectByCategoryId(@PathVariable Long categoryId){
        try {
            Result<List<Coupon>> result = couponService.selectByCategoryIdOrGoodsId(categoryId,"categoryId");
            return result;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/coupon/select/goods/{goodsId}")
    public Result<List<Coupon>> selectByGoodsId(@PathVariable Long goodsId){
        Result<List<Coupon>> result = null;
        try {
            result = couponService.selectByCategoryIdOrGoodsId(goodsId,"goodsId");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/coupon/create")
    public Result createCoupon(@CookieValue("adminTicket") String adminTicket,
                               @RequestBody CouponCreateVo couponCreateVo){
        Result result = couponService.insert(adminTicket,couponCreateVo);
        return result;
    }

    @ResponseBody
    @GetMapping("/coupon/select/noStart")
    public Result<List<Coupon>> selectByNoStart(@CookieValue("adminTicket") String adminTicket){
        Result<List<Coupon>> result = couponService.selectByNoStart(adminTicket);
        return result;
    }
}
